import base64
import json

import requests
from Crypto.PublicKey import RSA

from RSA加密.chaojiying import Chaojiying_Client
# pip install pycryptodome
from Crypto.Cipher import AES, PKCS1_v1_5


session = requests.session()
session.headers = {
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36'}

session.get('https://user.wangxiao.cn/login?url=https%3a%2f%2fk.wangxiao.cn%2fuser%2f')
img_text = session.post('https://user.wangxiao.cn/apis//common/getImageCaptcha',headers={
    'content-type':'application/json;charset=UTF-8',
    'referer':'https://user.wangxiao.cn/login?url=https%3a%2f%2fk.wangxiao.cn%2fuser%2f'
})
base_text = img_text.json()['data'].split(',')[-1]
with open('img.png','wb') as f:
    f.write(base64.b64decode(base_text))

# print(img_text.text)

chaojiying = Chaojiying_Client('18959118574', 'Foreverlove11.', '96001')	#用户中心>>软件ID 生成一个替换 96001
im = open('img.png', 'rb').read()													#本地图片文件路径 来替换 a.jpg 有时WIN系统须要//
pic_str = chaojiying.PostPic(im, 1902).get('pic_str')
print(pic_str)

public_key = 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDA5Zq6ZdH/RMSvC8WKhp5gj6Ue4Lqjo0Q2PnyGbSkTlYku0HtVzbh3S9F9oHbxeO55E8tEEQ5wj/+52VMLavcuwkDypG66N6c1z0Fo2HgxV3e0tqt1wyNtmbwg7ruIYmFM+dErIpTiLRDvOy+0vgPcBVDfSUHwUSgUtIkyC47UNQIDAQAB'

time_data = session.post('https://user.wangxiao.cn/apis//common/getTime',headers={
    'content-type':'application/json;charset=UTF-8',
    'referer':'https://user.wangxiao.cn/login?url=https%3a%2f%2fk.wangxiao.cn%2fuser%2f'
})
imageCaptchaCode="QHTA"
password= "123456"
userName="范德萨发"
pwdd = password + '' + time_data.json().get('data')

public_k = RSA.import_key(base64.b64decode(public_key))
rsa = PKCS1_v1_5.new(public_k)
mi_password = rsa.encrypt(pwdd.encode('utf-8'))
mi_password_base64 = base64.b64encode(mi_password).decode('utf-8')
print(mi_password_base64)

data  = {
    "imageCaptchaCode": pic_str,
    "password": "123456",
    "userName":"范德萨发"
}

res = session.post('https://user.wangxiao.cn/apis//login/passwordLogin',data=json.dumps(data,separators=(',',':'))
, headers = {
    'content-type': 'application/json;charset=UTF-8',
    'referer': 'https://user.wangxiao.cn/login?url=https%3a%2f%2fk.wangxiao.cn%2fuser%2f'
}
)
print(res.text)